package com.joey.protocol;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * Created by Administrator on 2016/7/22.
 */
public class ResponseError {

    private String status;
    private String message;
    // 上层解析出错
    public static final String ERROR_BY_PARSE = "500";
    // 服务器出错
    public static final String ERRPR_BY_SERVER = "400";
    // 服务器无响应，一般是超时
    public static final String ERRPR_NO_RESPONCE = "404";
    // 网络不可用
    public static final String ERRPR_NETWORK_UNREACHABLE = "300";
    // 离线
    public static final String ERROR_OFF_LINE = "004";
    // 成功
    public static final String ERROR_SUCCESS = JSONFormat.successKey;
    // 未知错误
    public static final String ERROR_UN_KNOWN = "-1";
    public JsonElement result;

    public ResponseError() {
        status = ERROR_SUCCESS;
        message = "";
    }

    /**
     * @param status
     * @param message
     * @param errorJson
     */
    public ResponseError(String status, String message, String errorJson) {
        this.status = status;
        this.message = message;
        try {
            this.result = new Gson().fromJson(errorJson, JsonObject.class);
        }catch (Exception e){
            this.result = new JsonObject();
        }

    }

    /**
     * @param errorJson
     */
    public ResponseError(String errorJson) {
        this(ResponseError.ERROR_UN_KNOWN, "", errorJson);
    }

    /**
     * @param status
     * @param message
     */
    public ResponseError(String status, String message) {
        this(status, message, "{}");
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public JsonElement getResult() {
        return result;
    }

    public void setResult(JsonElement result) {
        this.result = result;
    }

    @Override
    public String toString() {
        String returnStr = "";
        if (result != null) {
            returnStr = result.toString();
        }
        String str = String.format("{\"%s\":\"%s\",\"%s\":\"%s\",\"%s\":\"%s\"}",
                NetClientConfig.I.getJsonFormat().resultKey, returnStr,
                NetClientConfig.I.getJsonFormat().statusKey, status,
                NetClientConfig.I.getJsonFormat().msgKey, message);
        return str;
    }
}
